package org.ycx.upms.service.impl;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.ycx.common.annotation.BaseService;
import org.ycx.common.base.BaseServiceImpl;
import org.ycx.common.db.DataSourceEnum;
import org.ycx.common.db.DynamicDataSource;
import org.ycx.upms.dao.auto.SecPermissionMapper;
import org.ycx.upms.dao.mapper.ISecPermissionMapper;
import org.ycx.upms.dao.model.SecPermission;
import org.ycx.upms.dao.model.SecPermissionExample;
import org.ycx.upms.service.ISecPermissionService;

import java.util.List;

/**
 * SecPermissionService实现
 * Created by yang on 2017/6/26.
 */
@Service
@Transactional
@BaseService
public class SecPermissionServiceImpl extends BaseServiceImpl<SecPermissionMapper, SecPermission, SecPermissionExample> implements ISecPermissionService {

    private static Logger _log = LoggerFactory.getLogger(SecPermissionServiceImpl.class);

    @Autowired
    ISecPermissionMapper secPermissionMapper;

    @Override
    public List<SecPermission> findPermissionByUser(Integer uid,Integer sid) {
        try {
            DynamicDataSource.setDataSource(DataSourceEnum.SLAVE.getName());
            return this.secPermissionMapper.selectByUserName(uid,sid);
        } finally {
            DynamicDataSource.clearDataSource();
        }

    }

    @Override
    public int insert(SecPermission secPermission) {
        DynamicDataSource.setDataSource(DataSourceEnum.MASTER.getName());
        if (secPermission.getPid() == null) {
            secPermission.setCategory("1");//设置为模块
        } else {
            SecPermission parent = this.selectByPrimaryKey(secPermission.getPid());
            secPermission.setCategory((Integer.parseInt(parent.getCategory()) + 1) + "");
            secPermission.setSystemId(parent.getSystemId());
            secPermission.setState(parent.getState());
        }
        return super.insert(secPermission);
    }
}